# -*- coding: utf-8 -*-

# @Author: Chen Hua
# @Date  : 2020-08-19
# @Desc  : TWO COLOR BALL

import requests
from lxml import etree
from db.MySqlConn import DBSession, JobConfig, engine as eg, TwoColorBall
from util import DateUtil

##链接数据库
session = DBSession()


def get_url(url):  # 请求url的方法，返回html
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
    }
    response = requests.get(url, headers=headers)  # 获取请求的返回数据
    response.encoding = 'utf-8'  # 定义编码，不然中文输出会乱码；
    if response.status_code == 200:  # 如果请求成功，则返回；
        return response.text
    return None


def two_color_ball_info_sync():
    for q in range(1, 150):  # for循环，一共150页；
        url = 'http://kaijiang.zhcw.com/zhcw/html/ssq/list_%s.html' % (q)  # 定义请求的链接
        html = get_url(url)  # 请求url获取返回代码
        xpath_html = etree.HTML(html)  # xpath初始化html代码
        dates = xpath_html.xpath('//table[@class="wqhgt"]//tr//td[1]//text()')  # 获取开奖日期
        result = xpath_html.xpath('//table[@class="wqhgt"]//tr//em//text()')  # 获取上色球号
        issues = xpath_html.xpath('//table[@class="wqhgt"]//tr//td[2]//text()')  # 获取期号
        sta = 0
        end = 7
        for n in range(len(result) // 7):  # 双色球7个号一组，
            print("开奖日期:" + str(dates[n]) + " --- " + "期号:" + str(issues[n]) + " --- " + str(result[sta:end]))
            dt = DateUtil.yyyy_MM_hh_str_to_date(str(dates[n]))
            sn = str(issues[n])
            red = str(result[sta:end]).replace(']', '')
            red = red.replace("[", "")
            red = red.replace("'", "")
            red = red.split(",")
            wcb = TwoColorBall(dt=dt, sn=sn, r1=red[0], r2=red[1], r3=red[2], r4=red[3], r5=red[4], r6=red[5], b1=red[6])
            merged = session.merge(wcb)
            session.commit()
            sta = sta + 7
            end = end + 7


if __name__ == "__main__":
    two_color_ball_info_sync()
